import 'dart:async';
import 'package:flutter/material.dart';


class MyToast extends StatelessWidget {

  final Widget icon;
  final String tip;
  final double width;
  final double height;
  final double radius;
  final Duration duration;//自动关闭时间
  final BuildContext context;

  MyToast(this.tip,{
    this.icon,
    this.width:100.0,
    this.height:100.0,
    this.radius:10.0,
    this.duration,
    this.context,
  }){
    if(duration!=null)
    {
      Future.delayed(duration,(){
        Navigator.of(context).pop();
      });
    }
  }

  Widget _buildWidget(BuildContext context){

    if(icon!=null){
      return Padding(
        padding: EdgeInsets.all(8.0),
        child:Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children:[
            icon,
            SizedBox(height:8.0),
            Text(
              tip,
              maxLines: 1,
              overflow: TextOverflow.ellipsis
            ),
          ]
        )
      );
    }
    else{
      return Padding(
        padding: EdgeInsets.all(8.0),
        child:Text(
          tip,
          maxLines: 1,
          overflow: TextOverflow.ellipsis
        )
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child:Center(
        child:Material(
          color:Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(radius)),
          child:SizedBox(
            width: width,
            height: height,
            child:Center(
              child:_buildWidget(context),
            )
          ) 
        )
      )
    );
  }
}

Future<T> showMyToastDialog<T>(BuildContext context,String tip,{Widget icon,Duration duration}) async {
  return showDialog<T>(
    context: context,
    barrierDismissible: false, // user must tap button!
    builder: (BuildContext context) {
      return new MyToast(
        tip,
        icon:icon,
        context: context,
        duration: duration,
      );
    },
  );
}



